package handlers

import (
	"fmt"
	"net/http"
	"strconv"
	"web2/pkg/models"
	"web2/pkg/services"

	"github.com/gin-gonic/gin"
)

type ProductHandler struct {
	service services.ProductService
}

func NewProductHandler(service services.ProductService) *ProductHandler {
	return &ProductHandler{service: service}
}

func (h *ProductHandler) GetProducts(c *gin.Context) {
	products, err := h.service.GetAllProducts()
	fmt.Println(products)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.ProductResponse{
			Success: false,
			Message: "获取产品列表失败",
		})
		return
	}
	c.JSON(http.StatusOK, models.ProductResponse{
		Success:  true,
		Message:  "获取成功",
		Products: products,
	})
}

func (h *ProductHandler) GetProduct(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ProductResponse{
			Success: false,
			Message: "无效的产品ID",
		})
		return
	}

	product, err := h.service.GetProduct(uint(id))
	if err != nil {
		c.JSON(http.StatusNotFound, models.ProductResponse{
			Success: false,
			Message: "产品不存在",
		})
		return
	}

	c.JSON(http.StatusOK, models.ProductResponse{
		Success: true,
		Message: "获取成功",
		Data:    product,
	})
}

func (h *ProductHandler) CreateProduct(c *gin.Context) {
	var req models.CreateProductRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, models.ProductResponse{
			Success: false,
			Message: "无效的请求数据",
		})
		return
	}

	product, err := h.service.CreateProduct(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.ProductResponse{
			Success: false,
			Message: "创建产品失败",
		})
		return
	}

	c.JSON(http.StatusCreated, models.ProductResponse{
		Success: true,
		Message: "产品创建成功",
		Data:    product,
	})
}

func (h *ProductHandler) UpdateProduct(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ProductResponse{
			Success: false,
			Message: "无效的产品ID",
		})
		return
	}

	var req models.UpdateProductRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, models.ProductResponse{
			Success: false,
			Message: "无效的请求数据",
		})
		return
	}

	product, err := h.service.UpdateProduct(uint(id), req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, models.ProductResponse{
			Success: false,
			Message: "更新产品失败",
		})
		return
	}

	c.JSON(http.StatusOK, models.ProductResponse{
		Success: true,
		Message: "产品更新成功",
		Data:    product,
	})
}

func (h *ProductHandler) DeleteProduct(c *gin.Context) {
	id, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.JSON(http.StatusBadRequest, models.ProductResponse{
			Success: false,
			Message: "无效的产品ID",
		})
		return
	}

	if err := h.service.DeleteProduct(uint(id)); err != nil {
		c.JSON(http.StatusInternalServerError, models.ProductResponse{
			Success: false,
			Message: "删除产品失败",
		})
		return
	}

	c.JSON(http.StatusOK, models.ProductResponse{
		Success: true,
		Message: "产品删除成功",
	})
}
